/* timezone logic on setup page */
$(document).ready(function(){
	Timezone.init();
});

Timezone = {
	init:function(){
		$selects = $('select[name=stateID]');
		$selects.each(function(){
			var $stateID = $(this);
			var $form = $stateID.closest('form');
			var $timezone = $form.find('select[name=timezone]');
			if($form.length == 1 && $timezone.length == 1){
				$stateID.on('change',function(){
					var value = $stateID.val();
					var name = $stateID.find('option[value='+value+']').text();
					var $checkbox = $form.find("input[type=checkbox]");
					$timezone.val(Timezone.state[name]);
					Timezone.showHideDST();
					if(name == "AZ"){
						$checkbox.prop('checked',false);
					} else {
						$checkbox.prop('checked',true);
					}
					// select or not north america
					if(value != ""){
						$('select[name=continent]').val('NA').change();
					}
				});
			}
		});
		
		// refresh all the selects by calling onchange
		Timezone.showHideDST();
		
		// make a copy of timezone select
		Timezone.$timezone = $('select[name=timezone]:first').clone();
		// show timezone by continent
		Timezone.showTimezonesByContinent();
		$('select[name=continent]').on('change',function(){
			Timezone.showTimezonesByContinent();
		});
		
		Timezone.evaluateDST();
	},
	showHideDST:function(){
		$selects = $('select[name=stateID]');
		$selects.each(function(){
			var $stateID = $(this);
			var $form = $stateID.closest('form');
			var $timezone = $form.find('select[name=timezone]');
			if($form.length == 1 && $timezone.length == 1){
				var value = $stateID.val();
				var name = $stateID.find('option[value='+value+']').text();
				var $dst = $form.find('.daylight-savings');
				if(name == "AZ"){
					$dst.removeClass('hidden');
				} else {
					$dst.addClass('hidden');
				}
			}
		});	
	},
	state:{
		AL:"-6:00",	AK:"-9:00",	AZ:"-7:00",	AR:"-6:00",	CA:"-8:00",	CO:"-7:00",	CT:"-5:00",	DE:"-5:00",
		FL:"-5:00",	GA:"-5:00",	HI:"-10:00",ID:"-7:00",	IL:"-6:00",	IN:"-5:00",	IA:"-6:00",	KS:"-6:00",
		KY:"-5:00",	LA:"-6:00",	ME:"-5:00",	MD:"-5:00",	MA:"-5:00",	MI:"-5:00",	MN:"-6:00",	MS:"-6:00",
		MO:"-6:00",	MT:"-7:00",	NE:"-6:00",	NV:"-8:00",	NH:"-5:00",	NJ:"-5:00",	NM:"-7:00",	NY:"-5:00",
		NC:"-5:00",	ND:"-6:00",	OH:"-5:00",	OK:"-6:00",	OR:"-8:00",	PA:"-5:00",	RI:"-5:00",	SC:"-5:00",
		SD:"-6:00",	TN:"-6:00",	TX:"-6:00",	UT:"-7:00",	VT:"-5:00",	VA:"-5:00",	WA:"-8:00",	WV:"-5:00",
		WI:"-6:00",	WY:"-7:00", DC:"-5:00"
	},
	showContentSelectByState:function(){
		var $state = $('select[name=stateID]');
	},
	showTimezonesByContinent:function(){
		if($('select[name=continent]').length == 0){
			return;
		}
		var oldTimezone = $('select[name=timezone]').val();
		var continent = $('select[name=continent]').val();
		if(continent != ""){
			var $timezone = $('select[name=timezone]');
			$timezone.html('');
			$timezone.prepend($('<option value> - </option>'));
			for(var i = 0; i < Timezone.timezonesByContinent[continent].length; i++){
				var value = Timezone.timezonesByContinent[continent][i];
				var $option = Timezone.$timezone.find('option[value="'+value+'"]').clone();
				$timezone.append($option);
			}
		} else {
			$('select[name=timezone]').html(Timezone.$timezone.html());
		}
		$('select[name=timezone]').val(oldTimezone);
		$('.daylight-savings').removeClass('hidden');
	},
	timezonesByContinent:{
		AF:['-1:00','+0:00','+1:00','+2:00','+3:00','+4:00'],
		WA:['+3:00','+3:30','+4:00','+4:30','+5:00','+5:30','+5:45','+6:00'],
		AS:['+6:00','+6:30','+7:00','+8:00','+9:00','+10:00','+11:00','+12:00'],
		AU:['+5:00','+6:30','+7:00','+8:00','+8:45','+9:30','+10:00','+10:30','+11:30'],
		E:['-1:00','+0:00','+1:00','+2:00','+3:00'],
		NA:['-10:00','-9:00','-8:00','-7:00','-6:00','-5:00','-4:00','-3:30','-3:00'],
		SA:['-5:00','-4:30','-4:00','-3:00','-2:00']
	},
	evaluateDST:function(){
		// on each load, evaluate a dst range string and save to cookie.
		var dstString = "";
		var now = new Date();
		var startOfYear = new Date('01/01/'+now.getFullYear() + ' 08:00');
		// loop throught this year and record when dst starts and ends
		var dateIsDST = false; // toggleable var for building dst range string
		for(var i = 0; i < 365; i++){
			var thisDateString = DateTime.toString(startOfYear).substring(0,10) + ' 08:00';
			var thisDate = new Date(thisDateString);
			var thisDST = thisDate.dst();
			
			if(thisDST != dateIsDST){
				dateIsDST = !dateIsDST; // 
				if(thisDST == true){ // DST begins
					if(dstString != ""){
						dstString += "|";
					}
					dstString += DateTime.toString(thisDate).substring(0,5);
				} else { // DST ends
					dstString = dstString + '-' + DateTime.toString(thisDate).substring(0,5);
				}
			}
			startOfYear = DateTime.increment(startOfYear,1);
		}
		// at the end of the year, if there's no end range then end it
		if(dateIsDST){
			dstString = dstString + '-12/31';
		}
		Cookie.set('dstString',dstString,365);
	}
};